package com.koron.css2.newProjectCenter.impl;


import com.fasterxml.jackson.databind.JsonNode;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjTerminationBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjTerminationMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

public class PjTerminationSetmt implements ServerInterface {
	
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			String data = JsonUtils.objectToJson(req.getData());
			JsonNode jsonNode = JsonUtils.stringToJsonNode(data);
			PjInfoBean pjInfoBean = JsonUtils.objectToPojo(jsonNode.get("pjInfo"), PjInfoBean.class);
			PjTerminationBean termination = new PjTerminationBean();
			termination.setId(jsonNode.get("id").asText());
			termination.setComments(jsonNode.get("comments").asText());
			pjInfoBean.setId(jsonNode.get("pjId").asText());
			//判断是否需要结算
			if("1".equals(jsonNode.get("settlement").asText())) {
				//检查节点字段
				String checkResult = checkNode(pjInfoBean);
				if(!"OK".equals(checkResult)) {
					return MessageBean.create(Constant.MESSAGE_INT_FAIL, checkResult, null);
				}
				//根据是否结算押金计算退补金额
				if(pjInfoBean.getDepositBackFlag() == 1) {
					pjInfoBean.setBackFillMoney(
							(pjInfoBean.getSetmtMoney()==null?0:pjInfoBean.getSetmtMoney()) -
							(pjInfoBean.getAccruedMoney()==null?0.0:pjInfoBean.getAccruedMoney())
							);
				}else {
					pjInfoBean.setBackFillMoney(
							(pjInfoBean.getSetmtMoney()==null?0:pjInfoBean.getSetmtMoney()) -
							(pjInfoBean.getAccruedMoney()==null?0.0:pjInfoBean.getAccruedMoney()) +
							(pjInfoBean.getDepositMoney()==null?0:pjInfoBean.getDepositMoney())
							);
				}
				pjInfoBean.setUpdateName(userInfo.getUserInfo().getName());
				pjInfoBean.setUpdateAccount(userInfo.getUserInfo().getAcount());
				factory.getMapper(PjInfoMapper.class).savePreSetmt(pjInfoBean);
			}
			//更新终止说明
			factory.getMapper(PjTerminationMapper.class).update(termination);
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", null);
		}catch(Exception e){
			logger.error("工程终止结算编制失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "工程终止结算编制失败", null);
		}
	}
	
	public String checkNode(PjInfoBean pjInfoBean) {
		String checkResult = "OK";
		if(pjInfoBean.getSetmtLaborMeterBefore() == null) {
			return "表前人工费不能为空。";
		}
		if(pjInfoBean.getSetmtLaborMeterAfter() == null) {
			return "表后人工费不能为空。";
		}
		if(pjInfoBean.getSetmtMatrMeterBefore() == null) {
			return "表前材料费不能为空。";
		}
		if(pjInfoBean.getSetmtMatrMeterBefore() == null) {
			return "表后材料费不能为空。";
		}
		if(pjInfoBean.getSetmtDate() == null) {
			return "结算日期不能为空。";
		}
		if(pjInfoBean.getCostShare() == null) {
			return "总管分摊费用不能为空。";
		}
		if(pjInfoBean.getDepositBackFlag() == null) {
			return "是否退回押金不能为空。";
		}
		return checkResult;
	}

}
